

function isNullOrEmpty(str) {
	return str == null || str == '';
}

function FormValidator(formId) {
	this.formId = formId;
	this.result = true;
	
	this.getForm = function () {
		return document.forms[this.formId];
	};	
	
	this.getDOMId = function (formElementId) {
		formElementId = formElementId.replace('[', '-');
		formElementId = formElementId.replace(']', '');
		return formElementId;
	};
	
	this.clearErrors = function () {
		var form = this.getForm();
		for (var i = 0; i<form.length; i++) {
			if (!form[i].name) { continue; }			
			var element = document.getElementById(this.getDOMId(form[i].name));
			if (!element) { continue; }
			var errors = element.parentNode.getElementsByTagName("ul");
			if (errors.length > 0) {
				element.parentNode.removeChild(errors[0]);
			}
		}
	};	
	
	this.clearErrors();
	
	this.addError = function (elementId, message) {
		var form = this.getForm();
		if (elementId.indexOf("password", 0) == 0) {
			message = message.replace('_val_', '');
		} else {
			message = message.replace('_val_', form[elementId].value);
		}
		
		// simulate ZF conversion between element name with [] and element id in html
		elementId = this.getDOMId(elementId);
		var element = document.getElementById(elementId);
		var errors = element.parentNode.getElementsByTagName("ul");
		
		var error = document.createElement("li");
		error.appendChild(document.createTextNode(message));		
		
		if (errors.length > 0) {
			errors[0].appendChild(error);
		} else {
			var newErrors = document.createElement("ul");
			newErrors.setAttribute('class', 'errors');
			newErrors.appendChild(error);
			element.parentNode.appendChild(newErrors);
		}		
	};	
	
	this.isNotEmpty = function (elementId, message) {
		var form = this.getForm();
		if (isNullOrEmpty(form[elementId].value)) {
			this.addError(elementId, message);
			this.result = false;
		}
	};
	
	this.isNotShorterThan = function (elementId, message, length) {
		var form = this.getForm();
		if (isNullOrEmpty(form[elementId].value)) {
			return;
		}
		
		if (form[elementId].value.length < length) {
			this.addError(elementId, message);
			this.result = false;
		}
	};
	
	this.isNotLongerThan = function (elementId, message, length) {
		var form = this.getForm();
		if (isNullOrEmpty(form[elementId].value)) {
			return;
		}
		
		if (form[elementId].value.length > length) {
			this.addError(elementId, message);
			this.result = false;
		}
	};	
	
	this.isSameAs = function (elementId, message, otherElementId) {
		var form = this.getForm();
		if (isNullOrEmpty(form[elementId].value)) {
			return;
		}		
		
		if (form[elementId].value != form[otherElementId].value) {
			this.addError(elementId, message);
			this.result = false;
		}	
	};
	
	this.isEmail = function (elementId, message) {
		var form = this.getForm();
		if (isNullOrEmpty(form[elementId].value)) {
			return;
		}		
		
		var re = new RegExp("[a-zA-Z][a-zA-Z0-9]*@[a-zA-Z0-9]+(\.[a-zA-Z]{2,4})?");
		if (!form[elementId].value.match(re)) {
			this.addError(elementId, message);
			this.result = false;
		}		
	};
	
	this.isRealNumber = function (elementId, message) {
		var form = this.getForm();
		if (isNullOrEmpty(form[elementId].value)) {
			return;
		}		
		
		var re = new RegExp("^[0-9]+(\\.[0-9]*)?$");
		if (!form[elementId].value.match(re)) {
			this.addError(elementId, message);
			this.result = false;
		}
	};
};